/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_v_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i != 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d != 0;
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = !mpfr_zero_p(v->v.mpr);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_v_lt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i < 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d < 0;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_sgn(v->v.mpz) < 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_sgn(v->v.mpq) < 0;
	    break;
	case t_shr:	case t_mpr:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_less_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_v_le(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 1;
	    break;
	case t_int:
	    r0.v.i = v->v.i <= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d <= 0;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_sgn(v->v.mpz) <= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_sgn(v->v.mpq) <= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_v_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 1;
	    break;
	case t_int:
	    r0.v.i = v->v.i == 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d == 0;
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_zero_p(v->v.mpr);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_v_ge(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 1;
	    break;
	case t_int:
	    r0.v.i = v->v.i >= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d >= 0;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_sgn(v->v.mpz) >= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_sgn(v->v.mpq) >= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_v_gt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i > 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d > 0;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = mpz_sgn(v->v.mpz) > 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_sgn(v->v.mpq) > 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_greater_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_v_and(evalue_t *v)
{
    switch (v->t) {
	case t_void:		case t_int:
	case t_mpz:		case t_shz:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_and_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_or(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    r0.v.i = v->v.i;
	    break;
	case t_mpz:		case t_shz:
	    mpz_set(thr_zr, v->v.mpz);
	    set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_or_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_xor(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    r0.v.i = v->v.i;
	    break;
	case t_mpz:		case t_shz:
	    mpz_set(thr_zr, v->v.mpz);
	    set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_xor_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    r0.v.i = v->v.i;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d + 0.0;
	    break;
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_cqq:		case t_sqq:
	    r0.v.o = v->v.o;
	    r0.t = v->t;
	    break;
	case t_shr:	case t_mpr:
	    er_add_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd + 0.0;
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_i(thr_c, v->v.mpc, 0);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    r0.v.i = v->v.i;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d - 0.0;
	    break;
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_cqq:		case t_sqq:
	    r0.v.o = v->v.o;
	    r0.t = v->t;
	    break;
	case t_shr:	case t_mpr:
	    er_sub_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd - 0.0;
	    chk_rdd();
	    break;
	case t_shc:	case t_mpc:
	    ecc_sub_cc_i(thr_c, v->v.mpc, 0);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:		case t_int:
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_cqq:		case t_sqq:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d * 0.0;
	    break;
	case t_shr:	case t_mpr:
	    er_mul_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd * 0.0;
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_i(thr_c, v->v.mpc, 0);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:		case t_int:
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_cqq:		case t_sqq:
	    evm_raise(except_floating_point_error);
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d / 0.0;
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd / 0.0;
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_i(thr_c, v->v.mpc, 0);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:		case t_int:		case t_float:
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_mpr:		case t_shr:
	    evm_raise(except_floating_point_error);
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    /* should generate fpe or not a real number here */
	    evector_trunc_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    /* should not be executed */
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:		case t_int:
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	    evm_raise(except_floating_point_error);
	case t_float:
	    /* FIXME 0.0 or nan */
	    r0.t = t_float;
	    r0.v.d = fmod(v->v.d, 0.0);
	    break;
	case t_mpr:		case t_shr:
	    /* FIXME 0L or nan */
	    er_rem_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = 0.0;
	    }
	    else {
		mpfr_set_ui(thr_rr, 0, thr_rnd);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_i_d(v->v.i, 0.0);
	    }
	    else {
		er_atan2_i_i(thr_rr, v->v.i, 0);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_atan2_d_d(v->v.d, 0.0);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_z_d(v->v.mpz, 0.0);
	    }
	    else {
		er_atan2_z_i(thr_rr, v->v.mpz, 0);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_q_d(v->v.mpq, 0.0);
	    }
	    else {
		er_atan2_q_i(thr_rr, v->v.mpq, 0);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_i(thr_rr, v->v.mpr, 0);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_dd_d(v->v.cdd, 0.0);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_qq_d(v->v.cqq, 0.0);
		chk_rdd();
	    }
	    else {
		ecc_atan2_qq_i(thr_c, v->v.cqq, 0);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_i(thr_c, v->v.mpc, 0);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	case t_int:
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = 1.0;
	    }
	    else {
		mpfr_set_ui(thr_rr, 1, thr_rnd);
		set_rr();
	    }
	    break;
	case t_float:		case t_cdd:
	    r0.t = t_float;
	    r0.v.d = 1.0;
	    break;
	case t_mpr:		case t_shr:
	case t_mpc:		case t_shc:
	    mpfr_set_ui(thr_rr, 1, thr_rnd);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = 0.0;
	    }
	    else {
		mpfr_set_ui(thr_rr, 0, thr_rnd);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_i(v->v.i);
	    }
	    else {
		er_abs_i(thr_rr, v->v.i);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_abs_d(v->v.d);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_z(v->v.mpz);
	    }
	    else {
		er_abs_z(thr_rr, v->v.mpz);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_q(v->v.mpq);
	    }
	    else {
		er_abs_q(thr_rr, v->v.mpq);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_abs_r(thr_rr, v->v.mpr);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_float;
	    r0.v.d = ed_abs_dd(v->v.cdd);
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_qq(v->v.cqq);
	    }
	    else {
		er_abs_qq(thr_rr, v->v.cqq);
		set_rr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    er_abs_cc(thr_rr, v->v.mpc);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_int(v->v.o, 0, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_v_complex(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    r0.v.i = v->v.i;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d;
	    break;
	case t_mpz:		case t_shz:
	case t_mpq:		case t_shq:
	case t_mpr:		case t_shr:
	    r0.v.o = v->v.o;
	    r0.t = v->t;
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}
